﻿using System;
using System.Data;
using GZ.Entity;

public partial class Admin_AddProductInfo : ASPXBaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["User"].ToString() != "admin")
        {
            Response.Redirect("~/Admin/RedirectLogin.htm");
        }
        if (!IsPostBack)
        {
            LoadBind();
        }
    }

    void LoadBind()
    {
        DataTable dt = new GZ.Entity.ECategory().GetCategory();
        GZ.Common.DataBindHelper.BindDropDownList("CategoryName", "Id", dt, this.drpCategory);

        string id = GZ.Common.HttpHelper.GetParameter("id", GZ.Common.HttpHelper.HttpType.GET);
        if (!string.IsNullOrEmpty(id))
        {
            GZ.Entity.EProductInfo obj = new EProductInfo();
            obj = obj.GetProductInfoById(Convert.ToInt16(id));
            this.txtProductName.Text = obj.ProductName;
            this.txtProductEName.Text = obj.ProductEName;
            this.txtProductStyle.Text = obj.ProductStyle;
            this.txtProductPrice.Text = obj.Price.ToString();
            this.txtProductDescription.InnerText = obj.ProductDescription;
            this.drpCategory.SelectedValue = obj.CategoryId.ToString();
            this.drpIsShelves.SelectedValue = obj.IsShelves.ToString();
            this.txtOrderId.Text = obj.OrderId.ToString();
        }

    }

    /// <summary>
    /// 添加产品
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string id = GZ.Common.HttpHelper.GetParameter("id", GZ.Common.HttpHelper.HttpType.GET);
        if (string.IsNullOrEmpty(id))
        {
            GZ.Entity.EProductInfo obj = new EProductInfo();
            obj.ProductName = this.txtProductName.Text;
            obj.ProductEName = this.txtProductEName.Text;
            obj.ProductStyle = this.txtProductStyle.Text;
            obj.Price = Convert.ToDecimal(this.txtProductPrice.Text);
            obj.ProductDescription = this.txtProductDescription.Value;
            obj.CategoryId = Convert.ToInt16(this.drpCategory.SelectedValue);
            obj.IsShelves = Convert.ToInt16(this.drpIsShelves.SelectedValue);

            obj.OrderId = Convert.ToInt16(this.txtOrderId.Text);
            obj.Insert(obj);
        }
        else
        {
            GZ.Entity.EProductInfo obj = new EProductInfo();
            obj.Id = Convert.ToInt16(id);
            obj.ProductName = this.txtProductName.Text;
            obj.ProductEName = this.txtProductEName.Text;
            obj.ProductStyle = this.txtProductStyle.Text;
            obj.Price = Convert.ToDecimal(this.txtProductPrice.Text);
            obj.ProductDescription = this.txtProductDescription.Value;
            obj.CategoryId = Convert.ToInt16(this.drpCategory.SelectedValue);
            obj.IsShelves = Convert.ToInt16(this.drpIsShelves.SelectedValue);

            obj.OrderId = Convert.ToInt16(this.txtOrderId.Text);
            obj.Update(obj);
        }
        //关闭小窗，刷新父窗
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language=javascript>window.opener.location.href='ProductList.aspx';window.close();</script>");
    }
}